/**
 * 
 */
package com.cnhaimei.vod.logic;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.cnhaimei.base.TransparentActivity;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.bean.Room;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.NetManager;
import com.cnhaimei.vod.net.XVod;
import com.cnhaimei.vod.util.Const;
import com.cnhaimei.vod.util.Util;

/**
 * 密码
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-8-04
 * 
 */
public class PassCheckedActivity extends TransparentActivity implements EventsListener{
	private final static long TIME_OUT = 5 * 1000;
	private final static int MSG_TIME_OUT = 1;

	EditText pasV;
	View okV;
	View cancelV;
	Room room;
	int dcManagerId;
	String pas;
	boolean isOutTime;
	boolean isSilent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.room_dialog);
		
		String romStr = getIntent().getStringExtra(Const.Key.ROOM_KEY);
		
		if (romStr == null) {
			Toast.makeText(this, "", Toast.LENGTH_SHORT);
			finish();
		}
		room = new Room();
		room.setNum(romStr);
		pasV = (EditText) findViewById(R.id.pas);
		setClickable(R.id.ok, R.id.cancel);
		okV = findViewById(R.id.ok);
		cancelV = findViewById(R.id.cancel);
		XVod.G().registerListener(EventsManager.Event.CHCKD_PAS, this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (isFinishing()) {
			XVod.G().unRegisterListener(EventsManager.Event.CHCKD_PAS, this);
			XVod.G().unRegisterListener(EventsManager.Event.CONNECTING,this);
			XVod.G().unRegisterListener(EventsManager.Event.DISCONNECT,this);
		}
	}
	



	@Override
	public void onClick(View v) {
		// super.onClick(v);
		switch (v.getId()) {
		case R.id.ok: {
			pas = pasV.getText().toString();
			okV.setEnabled(false);
			cancelV.setEnabled(false);
			
			if (Util.TEST || Util.DEMO) {
				setResult(RESULT_OK);
				finish();
			} else {
				getRoomIp();
			}
		}
			break;
		case R.id.cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;

		default:
			break;
		}
	}
	
	private void sendTimeOutDelay() {
		myHandler.removeMessages(MSG_TIME_OUT);
		myHandler.sendEmptyMessageDelayed(MSG_TIME_OUT, TIME_OUT);
	}
	
	public void onEvent(int event, Object obj, boolean isSuc) {
		switch (event) {
		case EventsManager.Event.ROOM_INFO: {
			if (isSuc && obj instanceof Room) {
				if (!isOutTime) {
					Room r = (Room) obj;
					if (r.getNum().equals(room.getNum())) {
						XVod.G().unRegisterListener(
								EventsManager.Event.ROOM_INFO, this);
//						XVod.G().registerListener(
//								EventsManager.Event.CONNECTING, this);
						Log.e("RoomsActivity", "" + r.getIp());
						myHandler.removeMessages(MSG_TIME_OUT);
						XVod.G().registerListener(EventsManager.Event.CONNECTING,this);
						XVod.G().registerListener(EventsManager.Event.DISCONNECT,this);
						room.setIp(r.getIp());
						
						isSilent = true;
						NetManager.G().close(NetManager.G().getDCManangerId()); // 事先关闭
						dcManagerId = NetManager.G().startDCManager(r.getIp());
//						netRoom = r;
//						showEditDialog();
					}
				}
			} else {
				okV.setEnabled(true);
				cancelV.setEnabled(true);
				myHandler.removeMessages(MSG_TIME_OUT);
				Toast.makeText(this, R.string.get_room_fail, Toast.LENGTH_LONG);
			}
		}
		break;
		case EventsManager.Event.CHCKD_PAS: {
			if (isSuc) {
//				enter();
				setResult(RESULT_OK);
				XVod.G().startKeepPlayerHeart();
				finish();
			} else {
				okV.setEnabled(true);
				cancelV.setEnabled(true);
				
				myHandler.removeMessages(MSG_TIME_OUT);
				String s = getString(R.string.password_wrong);
				if (obj instanceof String) {
					s = (String)obj;
					if (s.equals("server not open")) {
						s = getString(R.string.server_not_open);
					} else if (s.startsWith("password error")) {
						s = getString(R.string.password_error);
					}
				}
			
				Toast.makeText(this, s,
								Toast.LENGTH_SHORT).show();
			}
		}
			break;
		case EventsManager.Event.CONNECTING: {
			if (obj != null && obj instanceof Integer) {
				int id = (Integer) obj;
				if (id == dcManagerId) {
					XVod.G().unRegisterListener(EventsManager.Event.CONNECTING,
							this);
					XVod.G().passwordCheck(pas);
				}
			} else {
				okV.setEnabled(true);
				cancelV.setEnabled(true);
			}
		}
		break;
		case EventsManager.Event.DISCONNECT: {
			if (obj != null && obj instanceof Integer) {
				int id = (Integer) obj;
				if (id == dcManagerId) {
					XVod.G().unRegisterListener(EventsManager.Event.DISCONNECT,
							this);
					okV.setEnabled(true);
					cancelV.setEnabled(true);
					if (!isSilent) {
						Toast.makeText(PassCheckedActivity.this,
								R.string.connecting_dc_error,
								Toast.LENGTH_SHORT).show();
						isSilent = false;
					}
				}
			}
		}
		break;
		
		default:
			break;
		}

	}
	
//	public void enter() {
//		startActivity(Const.Action.RoomMatched);
//		finish();
//	}
	
	private Handler myHandler = new  Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_TIME_OUT:
				isOutTime = true;
				okV.setEnabled(true);
				cancelV.setEnabled(true);
				Toast.makeText(PassCheckedActivity.this, R.string.read_room_error, Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		}
		
	};
	
	public void getRoomIp() {
		isOutTime = false;
		sendTimeOutDelay();
		// if (Util.DEMO || Util.TEST) {
		// enter();
		// // showEditDialog();
		// } else {
		XVod.G().registerListener(EventsManager.Event.ROOM_INFO, this);
		XVod.G().requestRoomInfo(room.getNum());
		// }
	}

}
